package com.changgou.service.goods.controller;


import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: Bytezero_zhengLei
 * @Date: 2024/8/26 8:44
 * @Project_Name: BrandController.java
 * @Email: 420498246@qq.com
 * @Description:
 * @TODO: 品牌
 */


@RequestMapping("/brand")
@RestController
public class BrandController {

    @Resource
    private BrandService brandService;

    /**
     * 查询
     *
     * @return brandList
     */
    @GetMapping
    public Result<List<Brand>> findList() {
        List<Brand> brandList = this.brandService.list();
//        return new Result<>(true, StatusCode.OK,"查询成功",brandList);
        return Result.success(brandList);
    }

    /**
     * id 查询
     *
     * @param id id
     * @return Brand
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable("id") Integer id) {
        Brand brand = brandService.getById(id);
        return Result.success(brand);
    }

    /**
     * 添加
     *
     * @param brand 数据
     * @return null
     */
    @PostMapping
    public Result<Void> add(@RequestBody Brand brand) {
        this.brandService.save(brand);
        return new Result<Void>(true, StatusCode.OK, "添加成功");
    }

    /**
     * @param id    修改id
     * @param brand 数据
     * @return void
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable("id") Integer id, @RequestBody Brand brand) {
        brand.setId(id);
        brandService.updateById(brand);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除id
     *
     * @param id 数据id
     * @return Void
     */
    @DeleteMapping("/{id}")
    public Result<Void> delById(@PathVariable("id") Integer id) {
        brandService.removeById(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    /**
     * 查询所有
     *
     * @param searchMap 数据
     * @return list
     */
    @GetMapping("/search")
    public Result<List<Brand>> search(@RequestParam Map<String, Object> searchMap) {
        List<Brand> list = brandService.search(searchMap);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 查询
     *
     * @param page 页面
     * @param size 大小
     * @return pageResult
     */

    @GetMapping("/search/{page}/{size}")
    public Result<Void> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageResult<Brand> pageResult = brandService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * @param searchMap 数据
     * @param page      页面
     * @param size      大小
     * @return pageResul
     */
    @GetMapping("/searchPage/{page}/{size}")
    public Result<PageResult<Brand>> findPage(@RequestParam Map<String, Object> searchMap, @PathVariable("page") int page, @PathVariable("size") int size) {
        PageResult<Brand> pageResul = brandService.queryPage(searchMap, page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageResul);
    }

    /**
     * 根据分类名称查询品牌列表
     * @param category
     * @return void
     */
    @GetMapping("/category/{category}")
    public Result<Void> findListByCategoryName(@PathVariable String category) {
        System.out.println(category);
        List<Map> brandList = brandService.findListByCategoryName(category);
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }





}
